﻿using System;
using System.Collections;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Windows.Forms;
namespace Cinser.Common.WinFrom
{
    public delegate void PageChangedEventHandler(object sender, EventArgs e);
    /// <summary>
    /// 分页工具条用户控件，仅提供分页信息显示及改变页码操作
    /// </summary>
    public class PageSplit : System.Windows.Forms.UserControl
    {
        public event PageChangedEventHandler PageChanged;
        private System.Windows.Forms.Label lblPageInfo;
        private System.Windows.Forms.TextBox txtCurrentPage;
        private int _pageSize;
        private int _pageCount;
        private int _recordCount;
        private int _currentPage;
        private System.Windows.Forms.Button btnFirst;
        private System.Windows.Forms.Button btnPrevious;
        private System.Windows.Forms.Button btnNext;
        private System.Windows.Forms.Button btnLast;
        /// <summary>
        /// 必需的设计器变量。
        /// </summary>
        private System.ComponentModel.Container components = null;
        /// <summary>
        /// 默认构造函数，设置分页初始信息
        /// </summary>
        public PageSplit()
        {
            // 该调用是 Windows.Forms 窗体设计器所必需的。
            InitializeComponent();
            // 在 InitializeComponent 调用后添加任何初始化
            this._pageSize = 10;
            this._recordCount = 0;
            this._currentPage = 1; //默认为第一页
            this.InitPageInfo();
        }
        /// <summary>
        /// 带参数的构造函数
        /// <param name="pageSize">每页记录数</param>
        /// <param name="recordCount">总记录数</param>
        /// </summary>
        public PageSplit(int recordCount, int pageSize)
        {
            // 该调用是 Windows.Forms 窗体设计器所必需的。
            InitializeComponent();
            // 在 InitializeComponent 调用后添加任何初始化
            this._pageSize = pageSize;
            this._recordCount = recordCount;
            this._currentPage = 1; //默认为第一页
            this.InitPageInfo();
        }

        /// <summary>
        /// 清理所有正在使用的资源。
        /// </summary>
        protected override void Dispose(bool disposing)
        {
            if (disposing)
            {
                if (components != null)
                {
                    components.Dispose();
                }
            }
            base.Dispose(disposing);
        }
        #region 组件设计器生成的代码
        /// <summary>
        /// 设计器支持所需的方法 - 不要使用代码编辑器
        /// 修改此方法的内容。
        /// </summary>
        private void InitializeComponent()
        {
            this.lblPageInfo = new System.Windows.Forms.Label();
            this.txtCurrentPage = new System.Windows.Forms.TextBox();
            this.btnNext = new System.Windows.Forms.Button();
            this.btnFirst = new System.Windows.Forms.Button();
            this.btnPrevious = new System.Windows.Forms.Button();
            this.btnLast = new System.Windows.Forms.Button();
            this.SuspendLayout();
            //
            // lblPageInfo
            //
            this.lblPageInfo.Location = new System.Drawing.Point(0, 4);
            this.lblPageInfo.Name = "lblPageInfo";
            this.lblPageInfo.Size = new System.Drawing.Size(264, 16);
            this.lblPageInfo.TabIndex = 0;
            this.lblPageInfo.Text = "共 {0} 条记录，每页 {1} 条，共 {2} 页";
            this.lblPageInfo.TextAlign = System.Drawing.ContentAlignment.MiddleRight;
            //
            // txtCurrentPage
            //
            this.txtCurrentPage.AutoSize = false;
            this.txtCurrentPage.Location = new System.Drawing.Point(336, 0);
            this.txtCurrentPage.Name = "txtCurrentPage";
            this.txtCurrentPage.Size = new System.Drawing.Size(25, 20);
            this.txtCurrentPage.TabIndex = 5;
            this.txtCurrentPage.Text = "1";
            this.txtCurrentPage.TextAlign = System.Windows.Forms.HorizontalAlignment.Right;
            this.txtCurrentPage.KeyDown += new System.Windows.Forms.KeyEventHandler(this.txtCurrentPage_KeyDown);
            //
            // btnNext
            //
            this.btnNext.Location = new System.Drawing.Point(364, 0);
            this.btnNext.Name = "btnNext";
            this.btnNext.Size = new System.Drawing.Size(30, 20);
            this.btnNext.TabIndex = 6;
            this.btnNext.Text = ">";
            this.btnNext.Click += new System.EventHandler(this.btnNext_Click);
            //
            // btnFirst
            //
            this.btnFirst.Location = new System.Drawing.Point(272, 0);
            this.btnFirst.Name = "btnFirst";
            this.btnFirst.Size = new System.Drawing.Size(30, 20);
            this.btnFirst.TabIndex = 7;
            this.btnFirst.Text = "|<";
            this.btnFirst.Click += new System.EventHandler(this.btnFirst_Click);
            //
            // btnPrevious
            //
            this.btnPrevious.Location = new System.Drawing.Point(304, 0);
            this.btnPrevious.Name = "btnPrevious";
            this.btnPrevious.Size = new System.Drawing.Size(30, 20);
            this.btnPrevious.TabIndex = 8;
            this.btnPrevious.Text = "<";
            this.btnPrevious.Click += new System.EventHandler(this.btnPrevious_Click);
            //
            // btnLast
            //
            this.btnLast.Location = new System.Drawing.Point(396, 0);
            this.btnLast.Name = "btnLast";
            this.btnLast.Size = new System.Drawing.Size(30, 20);
            this.btnLast.TabIndex = 9;
            this.btnLast.Text = ">|";
            this.btnLast.Click += new System.EventHandler(this.btnLast_Click);
            //
            // PageSplit
            //
            this.Controls.Add(this.btnLast);
            this.Controls.Add(this.btnPrevious);
            this.Controls.Add(this.btnFirst);
            this.Controls.Add(this.btnNext);
            this.Controls.Add(this.txtCurrentPage);
            this.Controls.Add(this.lblPageInfo);
            this.Cursor = System.Windows.Forms.Cursors.Hand;
            this.Name = "PageSplit";
            this.Size = new System.Drawing.Size(432, 24);
            this.ResumeLayout(false);
        }
        #endregion

        protected virtual void OnPageChanged(EventArgs e)
        {
            if (PageChanged != null)
            {
                PageChanged(this, e);
            }
        }

        /// <summary>
        /// 设置或获取每页记录数
        /// </summary>
        public int PageSize
        {
            set
            {
                this._pageSize = value;
            }
            get
            {
                return this._pageSize;
            }
        }
        /// <summary>
        /// 获取记录总页数
        /// </summary>
        public int PageCount
        {
            get
            {
                return this._pageCount;
            }
        }
        /// <summary>
        /// 设置或获取记录总数
        /// </summary>
        public int RecordCount
        {
            set
            {
                this._recordCount = value;
            }
            get
            {
                return this._recordCount;
            }
        }
        /// <summary>
        /// 设置或获取每页记录数
        /// </summary>
        public int CurrentPage
        {
            set
            {
                this._currentPage = value;
            }
            get
            {
                return this._currentPage;
            }
        }
        /// <summary>
        /// 初始化分页信息
        /// <param name="pageSize">每页记录数</param>
        /// <param name="recordCount">总记录数</param>
        /// </summary>
        public void InitPageInfo(int recordCount, int pageSize)
        {
            this._recordCount = recordCount;
            this._pageSize = pageSize;
            this.InitPageInfo();
        }
        /// <summary>
        /// 初始化分页信息
        /// <param name="recordCount">总记录数</param>
        /// </summary>
        public void InitPageInfo(int recordCount)
        {
            this._recordCount = recordCount;
            this.InitPageInfo();
        }
        /// <summary>
        /// 初始化分页信息
        /// </summary>
        public void InitPageInfo()
        {
            if (this._pageSize < 1)
                this._pageSize = 10; //如果每页记录数不正确，即更改为10
            if (this._recordCount < 0)
                this._recordCount = 0; //如果记录总数不正确，即更改为0

            //取得总页数
            if (this._recordCount % this._pageSize == 0)
                this._pageCount = this._recordCount / this._pageSize;
            else
                this._pageCount = this._recordCount / this._pageSize + 1;
            //设置当前页
            if (this._currentPage > this._pageCount)
                this._currentPage = this._pageCount;
            if (this._currentPage < 1)
                this._currentPage = 1;

            this.txtCurrentPage.Text = this._currentPage.ToString();
            this.lblPageInfo.Text = string.Format("共 {0} 条记录，每页 {1} 条，共 {2} 页", this._recordCount, this._pageSize, this._pageCount);
        }

        public void RefreshData(int page)
        {
            this._currentPage = page;
            this.txtCurrentPage.Text = this._currentPage.ToString();
            EventArgs e = new EventArgs();
            OnPageChanged(e);
        }
        private void btnFirst_Click(object sender, System.EventArgs e)
        {
            this.RefreshData(1);
        }
        private void btnPrevious_Click(object sender, System.EventArgs e)
        {
            if (this._currentPage > 1)
                this.RefreshData(this._currentPage - 1);
            else
                this.RefreshData(1);
        }
        private void btnNext_Click(object sender, System.EventArgs e)
        {
            if (this._currentPage < this._pageCount)
                this.RefreshData(this._currentPage + 1);
            else if (this._pageCount < 1)
                this.RefreshData(1);
            else
                this.RefreshData(this._pageCount);
        }
        private void btnLast_Click(object sender, System.EventArgs e)
        {
            if (this._pageCount > 0)
                this.RefreshData(this._pageCount);
            else
                this.RefreshData(1);
        }
        private void txtCurrentPage_KeyDown(object sender, System.Windows.Forms.KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                int num;
                try
                {
                    num = Convert.ToInt16(this.txtCurrentPage.Text);
                }
                catch (Exception ex)
                {
                    num = 1;
                }
                if (num > this._pageCount)
                    num = this._pageCount;
                if (num < 1)
                    num = 1;
                this.RefreshData(num);
            }
        }
    }
}